﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using Microsoft.JCI.Common.Utility;
using System.Collections.Generic;
using Microsoft.JCI.Bll;
using Microsoft.JCI.Common.Entity;

namespace Microsoft.JCI.Portal.Services
{
    /// <summary>
    /// Summary description for UserService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class UserService : System.Web.Services.WebService
    {
        [WebMethod(true)]
        public LoginServiceEntity Login(string Account, string Password)
        { 
            LoginServiceEntity ls = new LoginServiceEntity();

            List<SqlDbParameter> parms = new List<SqlDbParameter>();
            SqlDbParameter parm = new SqlDbParameter();
            parm.ColumnName = "Account";
            parm.ColumnType = DbType.String;
            parm.ParameterName = "@Account";
            parm.ParameterValue = Account;
            parm.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
            parms.Add(parm);

            parm = new SqlDbParameter();
            parm.ColumnName = "Password";
            parm.ColumnType = DbType.String;
            parm.ParameterName = "@Password";
            parm.ParameterValue = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Password, "MD5");
            parm.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
            parms.Add(parm);

            SystemBll bll = new SystemBll();
            SysUserEntity entity = bll.Get<SysUserEntity>(parms);
            if (entity != null)
            {
                ls.IsLogin = true;

                parms = new List<SqlDbParameter>();
                parm = new SqlDbParameter();
                parm.ColumnName = "UserID";
                parm.ColumnType = DbType.Guid;
                parm.ParameterName = "@UserID";
                parm.ParameterValue = entity.UserID;
                parm.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
                parms.Add(parm);
                entity.UserRoleRelationList = new SystemBll().GetAll<SysUserRoleRelationEntity>(parms);

                Session[DeclareHelper.SessionConstant.CurrentUserID] = entity.UserID;
                Session[DeclareHelper.SessionConstant.CurrentUserEntity] = entity;
            }
            else
            {
                ls.IsLogin = false;
                ls.MessageUI = "用户名或密码错误!";
            }
            return ls;
        }

    }
}
